package com.xrui.hbase.schema.impl;

import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.xrui.hbase.compression.NoneCompressionProvider;
import com.xrui.hbase.conversion.RawBytesCellConverter;
import com.xrui.hbase.schema.CellSchema;
import com.xrui.hbase.schema.SchemaType;

public class GenericCellSchema implements CellSchema {
    SchemaType mSchemaType;
    String mValue;
    String mCompressionProvider;
    String mConverter;

    GenericCellSchema(SchemaType schemaType, String value, String compressionProvider, String converter) {
        mSchemaType = schemaType;
        //mValue = Preconditions.checkNotNull(value);
        mValue =  value;
        if(null == converter || converter.trim().isEmpty()) {
            mConverter = RawBytesCellConverter.PROVIDER_NAME;
        } else {
            mConverter = converter;
        }
        mCompressionProvider = null == compressionProvider ? NoneCompressionProvider.PROVIDER_NAME : compressionProvider;
    }

    public static GenericCellSchema create(SchemaType schemaType, String value, String compressionProvider, String converter) {
        return new GenericCellSchema(schemaType, value, compressionProvider, converter);
    }

    @Override
    public SchemaType getType() {
        return mSchemaType;
    }

    @Override
    public String getValue() {
        return mValue;
    }

    @Override
    public String getCompression() {
        return mCompressionProvider;
    }

    @Override
    public String getConverter() {
        return mConverter;
    }

    @Override
    public  String toString() {
        return Objects.toStringHelper(GenericCellSchema.class)
            .add("type", mSchemaType)
            .add("value", mValue)
            .add("converter", mConverter)
            .add("compression", mCompressionProvider)
            .toString();
    }
}
